package com.cpkso.sprdata.web;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.Part;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

/**
 * spring mvc doc example
 * @author wrzhxy@qq.com
 * @date 2017年11月9日
 */
@Controller
public class FileUploadController {

        @PostMapping("/form1")
        public String handleFormUpload(@RequestParam("name") String name,
                @RequestParam("file") MultipartFile file) throws IOException {

            if (!file.isEmpty()) {
//                byte[] bytes = file.getBytes();
                file.transferTo(new File("D:/snowpics/a.jpg"));
                // store the bytes somewhere
                return "redirect:lue/g/1";
            }

            return "redirect:uploadFailure";
        }
        
        @PostMapping("/form2")
        public String handleFormUpload(@RequestParam("name") String name,
                @RequestParam("file") Part file) throws IOException {

        	file.write("D:/snowpics/b.jpg");
//            InputStream inputStream = file.getInputStream();
            // store bytes from uploaded file somewhere

            return "redirect:lue/g/1";
        }
        @PostMapping("/form3")
        public String handleFormUpload(@RequestParam("name") String name,
        		@RequestParam("file") Part[] files) throws IOException {
        	
//        	file.write("D:/snowpics/b.jpg");
        	for (int i = 0; i < files.length; i++) {
        		files[i].write("D:/snowpics/" + files[i].getSubmittedFileName());
        	}
//            InputStream inputStream = file.getInputStream();
        	// store bytes from uploaded file somewhere
        	
        	return "redirect:lue/g/1";
        }
        
//        @RequestMapping(path="/error")
//        public String error() {
//        	return "index.html";
//        }
}
